package com.seguriapp.controller;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.seguriapp.configuration.GlobalConfigLoader;
import com.seguriapp.entities.Paperwork;
import com.seguriapp.services.PaperworkService;
import com.seguriapp.util.Util;

/**
 * Represents the controller for the paperwork entities
 * 
 * @author Tania Gonzales(taniadgv@gmail.com)
 */
@Controller
@RequestMapping("/paperwork")
public class PaperworkController {

  private static final Logger LOGGER = Logger.getLogger(PaperworkController.class);

  @Autowired
  private PaperworkService procedureService;

  @RequestMapping(value = "/list", method = RequestMethod.GET)
  @ResponseBody()
  public String getPaperworkList() {
    final List<Paperwork> paperworkList = procedureService.listActivePaperwork();
    if (paperworkList != null) {
      LOGGER.info("Size of paperworks list : " + paperworkList.size());
      return Util.parseEntityListToJson("paperworks", paperworkList);
    }
    return StringUtils.EMPTY;
  }

  @RequestMapping(value = "/getPaperworkFile/{paperworkId}/{dni}", method = RequestMethod.GET)
  @ResponseBody()
  public String getPaperworkFilePath(@PathVariable("paperworkId") Long paperworkId, @PathVariable("dni") String dni) {
    LOGGER.info("request for paperwork id: " + paperworkId + " dni: " + dni);
    return String.format(GlobalConfigLoader.getInstance().getGlobalConfig().getPaperworkServer(), paperworkId, dni);
  }

}
